package com.draven.redis.controller;

import com.draven.redis.entity.User;
import com.draven.redis.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/user")
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Long id) {
        User user = userService.getUserById(id);
        return user;
    }

    @RequestMapping("/findUser/{id}")
    public User findUserById(@PathVariable("id") Long id) {
        Optional<User> optional = userService.findUserById(id);
        return optional.orElse(null);
    }

    @PostMapping("/updateUser")
    public void updateUser(@RequestBody User user) {
        userService.updateUser(user);
    }

    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Long id) {
        userService.deleteUser(id);
        return "删除成功";
    }
}
